<?php
/**
 * User: 魔十七
 * Date: 2020/11/6
 * Time: 16:16
 */

namespace Merchant\Controller;
use Think\Controller;
//商家后台登录
class LoginController extends Controller
{
    public function Login(){
        $this->display();
    }

    /**
     * 生成验证码
     */
    public function verify(){
        $config =    array(
            'fontSize'    =>    30,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
        );
        $Verify =     new \Think\Verify($config);
        $Verify->entry();
    }

    //商家登录
    public function save(){
        $username = isset($_POST['username']) ? trim($_POST['username']) : '';
        $password = isset($_POST['password']) ? trim($_POST['password']) : '';
        $code = isset($_POST['code']) ? trim($_POST['code']) : '';
        if ($username && $password == '') {
            $arr = array(
                "code"=>4,
                "msg"=>"用户名密码必填",
            );
            $this->ajaxReturn($arr);
        }
        if (!$this->check_verify($code)) {
            $arr = array(
                "code"=>3,
                "msg"=>"验证码错误",
            );
            $this->ajaxReturn($arr);
        }
        $model = M("merchant");
        $user = $model->where(array('tel' => $username, 'pwd' => md5($password)))->find();
        if($user){
            session('merchant',$user);
            cookie('merchant',$user);
            $arr = array(
                "code"=>0,
                "msg"=>"登录成功",
            );
        }else{
            $arr = array(
                "code"=>"1",
                "msg"=>"您输入的账号或密码不对",
            );
        }
        $this->ajaxReturn($arr);
    }

    /**
     * @param $code
     * @param string $id
     * @return bool
     * 验证码检测
     */
    function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }
}